package edu.gatech.cs2340.sharks.fms;

/**
 * RegisterActivity.java
 * @author Steven Mitchell
 * 
 * Allows a user to register an account.
 */

import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import edu.gatech.cs2340.sharks.fms.data.User;

public class RegisterActivity extends Activity
{
	/**
	 * The success message that is sent to the login activity upon a successful registration.
	 */
	public final static String SUCCESS_MESSAGE = "edu.gatech.cs2340.sharks.fms.SUCCESS_MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
	}

	/**
	 * Obtains all of the information from user and creates a user in the database.
	 * If successful, the user returns to the login page with a success message.
	 * Else, display an error message.
	 * 
	 * @param view The submit button
	 */
	public void submitRegister(View button)
	{
		final EditText nameField = (EditText) findViewById(R.id.editTextName);
		String name = nameField.getText().toString();
		final EditText userField = (EditText) findViewById(R.id.editTextUsername);
		String username = userField.getText().toString();
		final EditText passField = (EditText) findViewById(R.id.editTextPass);
		String password = passField.getText().toString();
		Status success = DBMgr.createUser(username, password, name, User.Role.CLIENT, "null@example.com", "Nowehere", "Sample profile");
		final TextView statusField = (TextView) findViewById(R.id.textViewStatus);
		if (success.code() == Status.Code.OK)
		{
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra(SUCCESS_MESSAGE, "Registration Successful! Please log in."); 
			startActivity(intent);
		}
		else statusField.setText(success.toString());
	}
}
